package com.kingzoon.seckill.service.impl.base;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kingzoon.seckill.base.ehcache.EHCacheTemplate;
import com.kingzoon.seckill.service.ClientService;
import com.kingzoon.seckill.service.OAuthService;

@Service
public class OAuthServiceImpl implements OAuthService {

	private EHCacheTemplate ct = EHCacheTemplate.getCache();

	@Autowired
	private ClientService clientService;

	public void addAuthCode(String authCode, String username) {
		ct.put(authCode, username);
	}

	public void addAccessToken(String accessToken, String username) {
		ct.put(accessToken, username);
	}

	public String getUsernameByAuthCode(String authCode) {
		return (String) ct.get(authCode);
	}

	public String getUsernameByAccessToken(String accessToken) {
		return (String) ct.get(accessToken);
	}

	public boolean checkAuthCode(String authCode) {
		return ct.get(authCode) != null;
	}

	public boolean checkAccessToken(String accessToken) {
		return ct.get(accessToken) != null;
	}

	public boolean checkClientId(String clientId) {
		return clientService.findByClientId(clientId) != null;
	}

	public boolean checkClientSecret(String clientSecret) {
		return clientService.findByClientSecret(clientSecret) != null;
	}

	public long getExpireIn() {
		return 3600L;
	}
}
